﻿/*
============================================================================
文件名    ： base.js
完全限定名： /scripts/base.js
作者      ： 陈存腾
创建时间  ： 2011-12-26
修改时间  ： 
版本      ：
说明      ： 一些基础常用的js函数
============================================================================
*/


/************************************************************************/
/* Cookie操作                                                           */
/************************************************************************/

var Cookie = {
    /* 获得Cookie值 */
    get: function (c_name) {
        if (document.cookie.length > 0) {
            /* 先查询cookie是否为空，为空就return "" */
            c_start = document.cookie.indexOf(c_name + "=");
            /* 通过String对象的indexOf()来检查这个cookie是否存在，不存在就为 -1 */
            if (c_start != -1) {
                /* 获取到了cookie值的起始位置和末尾位置 */
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                /* 提取并解码Cookie值 */
                if (c_end == -1) {
                    c_end = document.cookie.length;
                }
                return DecodeURI(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    },
    /* 设定Cookie值 */
    set: function (c_name, value, expiredays) {
        var exdate = new Date()
        exdate.setDate(exdate.getDate() + expiredays)
        document.cookie = c_name + "=" + EncodeURI(value) +
            ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    },
    /* 删除Cookie */
    del: null
};
Cookie.del = function (name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = Cookie.get(name);
    document.cookie = name + "=" + cval + ";  expires=" + exp.toGMTString();
}

/************************************************************************/
/* 浏览器相关                                                           */
/************************************************************************/

// 获取滚动条位置
document.getPageScroll = function () {
    var x, y;
    if (window.pageYOffset) {
        // 其它浏览器
        y = window.pageYOffset;
        x = window.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        // IE6
        y = document.documentElement.scrollTop;
        x = document.documentElement.scrollLeft;
    }
    else if (document.body) {
        // 其它IE版本
        y = document.body.scrollTop;
        x = document.body.scrollLeft;
    }
    return { X: x, Y: y };
}

// 得到浏览器显示的屏幕高度
document.getViewportHeight = function () {
    if (window.innerHeight != window.undefined) return window.innerHeight;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight;
    return window.undefined;
}

// 得到浏览器显示的屏幕宽度
document.getViewportWidth = function () {
    if (window.innerWidth != window.undefined) return window.innerWidth;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientWidth;
    if (document.body) return document.body.clientWidth;
}

/************************************************************************/
/* 字符串操作                                                           */
/************************************************************************/

// 去掉字符串首尾空格
String.prototype.trim = function () {
    return (this || "").replace(/^\s+|\s+$/g, "");
}


/************************************************************************/
/* 问候语                                                               */
/************************************************************************/

// 根据时间获取问候语
function getGreetings() {
    var day = new Date()
    var hr = day.getHours();
    var greetings;
    switch (hr) {
        case 1:
            greetings = "凌晨一点已过，别忘了休息喔！";
            break;
        case 2:
            greetings = "该休息了，身体可是革命的本钱啊！";
            break;
        case 3:
            greetings = "夜深人静，只有你敲击鼠标的声音...";
            break;
        case 4:
            greetings = "四点过了，你明天不上课？？？";
            break;
        case 5:
            greetings = "该去晨运了！！！";
            break;
        case 6:
            greetings = "你知道吗，此时是国内网络速度最快的时候！";
            break;
        case 7:
            greetings = "新的一天又开始了，祝你过得快乐！";
            break;
        case 8:
        case 9:
        case 10:
            greetings = "上午好！今天你看上去好精神哦！";
            break;
        case 11:
            greetings = "十一点过了，快下班了吧？";
            break;
        case 12:
            greetings = "十二点过了，还不下班？";
            break;
        case 13:
        case 14:
            greetings = "你不睡午觉？";
            break;
        case 15:
        case 16:
        case 17:
            greetings = "下午好！";
            break;
        case 18:
        case 19:
            greetings = "吃晚饭了吧？";
            break;
        case 20:
        case 21:
        case 22:
            greetings = "今晚又在这玩电脑了，没节目？";
            break;
        case 23:
            greetings = "真是越玩越精神，不打算睡了？";
            break;
        case 0:
            greetings = "看来大家都是夜猫子(^o^) ";
            break;
    }
    return greetings;
}


/************************************************************************/
/* 实现C#的编码解码                                                     */
/************************************************************************/

var EncodeURI = function (unzipStr, isCusEncode) {
    if (isCusEncode) {
        var zipArray = new Array();
        var zipstr = "";
        var lens = new Array();
        for (var i = 0; i < unzipStr.length; i++) {
            var ac = unzipStr.charCodeAt(i);
            zipstr += ac;
            lens = lens.concat(ac.toString().length);
        }
        zipArray = zipArray.concat(zipstr);
        zipArray = zipArray.concat(lens.join("O"));
        return zipArray.join("N");
    }
    else {
        //return encodeURI(unzipStr);
        var zipstr = "";
        var strSpecial = "!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
        var tt = "";

        for (var i = 0; i < unzipStr.length; i++) {
            var chr = unzipStr.charAt(i);
            var c = StringToAscii(chr);
            tt += chr + ":" + c + "n";
            if (parseInt("0x" + c) > 0x7f) {
                zipstr += encodeURI(unzipStr.substr(i, 1));
            }
            else {
                if (chr == " ")
                    zipstr += "+";
                else if (strSpecial.indexOf(chr) != -1)
                    zipstr += "%" + c.toString(16);
                else
                    zipstr += chr;
            }
        }
        return zipstr;
    }
}

var DecodeURI = function (zipStr, isCusEncode) {
    if (isCusEncode) {
        var zipArray = zipStr.split("N");
        var zipSrcStr = zipArray[0];
        var zipLens;
        if (zipArray[1]) {
            zipLens = zipArray[1].split("O");
        }
        else {
            zipLens.length = 0;
        }

        var uzipStr = "";

        for (var j = 0; j < zipLens.length; j++) {
            var charLen = parseInt(zipLens[j]);
            uzipStr += String.fromCharCode(zipSrcStr.substr(0, charLen));
            zipSrcStr = zipSrcStr.slice(charLen, zipSrcStr.length);
        }
        return uzipStr;
    } else {
        //return decodeURI(zipStr);
        var uzipStr = "";

        for (var i = 0; i < zipStr.length; i++) {
            var chr = zipStr.charAt(i);
            if (chr == "+") {
                uzipStr += " ";
            }
            else if (chr == "%") {
                var asc = zipStr.substring(i + 1, i + 3);
                if (parseInt("0x" + asc) > 0x7f) {
                    uzipStr += decodeURI("%" + asc.toString() + zipStr.substring(i + 3, i + 9).toString()); ;
                    i += 8;
                }
                else {
                    uzipStr += AsciiToString(parseInt("0x" + asc));
                    i += 2;
                }
            }
            else {
                uzipStr += chr;
            }
        }
        return uzipStr;
    }
}

var StringToAscii = function (str) {
    return str.charCodeAt(0).toString(16);
}

var AsciiToString = function (asccode) {
    return String.fromCharCode(asccode);
}


